import Vue from 'vue'
import Vuex from 'vuex'
import allRoutes from '@/router/allRouter.js'
import router from '@/router'
import recursionRoutes from '../uitils/recursionRoutes'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    sideMenu: [],
    crumb: []
  },
  mutations: {
    SET_SIDEMENU (state, payload) {
      state.sideMenu = payload
      const target = router.options.routes.find(item => item.path === '/')
      target.children = [...state.sideMenu]
      router.addRoutes(router.options.routes)
    },
    CLEAR_SIDEMENU (state) {
      state.sideMenu = []
    },
    SET_CRUMB (state, payload) {
      state.crumb = payload
    }
  },
  actions: {
    async FETCH_MENULIST ({ commit }) {
      const res = await this.$http.get('/permission/getMenuList')
      if (res && res.data) {
        const userMenu = recursionRoutes(allRoutes, res.data.menuList)
        commit('SET_SIDEMENU', userMenu)
      }
    }
  }

})
